package com.lhfmqc.controller;


import com.lhfmqc.pojo.Books;
import com.lhfmqc.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import java.util.List;


/**
 * @author lhfmqc
 * @date 2021/10/1/001 16:34
 */
@Controller
@RequestMapping(value ="/book")
public class BookController {
    @Resource
    private BookService bookService;

    @RequestMapping(value="test")
    public String test(){
        return "hello world";
    }

    @RequestMapping(value = "allBook")
    public String list(Model model){
        List<Books> list = bookService.queryAllBook();
        model.addAttribute("list",list);
        return "/allBook";
    }

    public List<Books> queryAllBook(){
        List<Books> list = bookService.queryAllBook();
        return list;
    }

    @RequestMapping(value = "/toAddBook")
    public String toAddPager(){
        return "addBook";
    }

    @RequestMapping("/addBook")
    public String addPage(Books book){
        bookService.addBook(book);
        return "redirect:/book/allBook";
    }

    @RequestMapping("/toUpdateBook")
    public String toUpdateBook(Model model, int id){
        Books book = bookService.queryBookById(id);
        model.addAttribute("book",book);
        return "updateBook";
    }

    @RequestMapping("/updateBook")
    public String updateBook(Model model, Books book){
        bookService.updateBook(book);
        Books books = bookService.queryBookById(book.getBookID());
        model.addAttribute("books",books);
        return "redirect:/book/allBook";
    }

    @RequestMapping("/del{bookId}")
    public String deleteBook(@PathVariable("bookId") int id){
        bookService.deleteBookById(id);
        return "redirect:/book/allBook";
}
}
